package tom.spring.cloud.integration.sender;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.DateUtils;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.core.MessageSource;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageBuilder;
import tom.spring.cloud.msg.MyInput;
import tom.spring.cloud.msg.vo.Alien;
import tom.spring.cloud.msg.vo.Dog;
import tom.spring.cloud.msg.vo.User;
import tom.spring.cloud.msg.MyOutput;

import java.util.Date;

/**
 * 自定义output
 * @author ZHUFEIFEI
 */
@Slf4j
@EnableBinding({MyOutput.class})
public class Sender02 {


    @Bean
    @InboundChannelAdapter(value = MyOutput.OUTPUT, poller = @Poller(fixedRate = "2000"))
    public MessageSource<String> string() {
        String str = "str_" + System.currentTimeMillis();
        return () -> { log.info("output02 String: {}", str);return new GenericMessage<>(str);};
    }
    /**
     * 每2秒向指定topic发送时间信息
     * @return
     */
    @Bean
    @InboundChannelAdapter(value = MyOutput.OUTPUT, poller = @Poller(fixedRate = "2000"))
    public MessageSource<Date> time02() {
        Date dt = new Date();
        return () -> { log.info("output02 date: {}", dt);return new GenericMessage<>(dt);};
    }

    @Bean
    @InboundChannelAdapter(value = MyOutput.OUTPUT, poller = @Poller(fixedRate = "2000"))
    public MessageSource<User> user01() {
        User u = new User();
        u.setName("tom");
        u.setAge(10);
        return () -> { log.info("output02 user : {}", u);return new GenericMessage<>(u);};
    }

    @Bean
    @InboundChannelAdapter(value = MyOutput.OUTPUT, poller = @Poller(fixedRate = "2000"))
    public MessageSource<Dog> dog01() {
        Dog u = new Dog();
        u.setName("dog");
        u.setColor("red");
        return () -> { log.info("output02 dog for transform : {}", u);return new GenericMessage<>(u);};
    }

    @Bean
    @InboundChannelAdapter(value = MyOutput.OUTPUT, poller = @Poller(fixedRate = "2000"))
    public MessageSource<Alien> alien() {
        Alien u = new Alien();
        u.setUniverse("solar");
        u.setDistance(10);
        return () -> { log.info("output02 alien : {}", u);
           return MessageBuilder.withPayload(u).setHeader("region", "galaxy").build();};
    }

    /**
     * 实验发现@ServiceActivator可以接收并转换为参数需要的类型, 默认使用SimpleTypeConvert。
     * 通过Transformer注解可以将一种类型转换为另一种类型，
     *
     * inputChannel ： 将要被转换的消息来源channel
     * outputChannel：转换后的发到哪个channel上
     *
     * 此处消息转换混乱，接收到的消息类型为application/json，simpleTypeConvert会自动将他们转成参数类型,是json的都可能走到该方法
     * ，导致混乱 比如dog和user，转换出的对象不对，应该是同一个topic队列中多类型消息不被支持的，一般实际应用一个队列对应
     * 一种消息payload，多种时候可以用header区分.
     *
     * @param dog
     * @return
     */
    @Transformer(inputChannel = MyOutput.OUTPUT, outputChannel = MyOutput.OUTPUT)
    public String transformDogOutput(Dog dog) {
        log.info("transformDog => {}", dog);
        return "this is " + dog.getName() + " color " + dog.getColor();
    }

    @Transformer(inputChannel = MyOutput.OUTPUT, outputChannel = MyOutput.OUTPUT)
    public String transformDateOutput(Date date) {
        log.info("transformDate => {}", date);
        return DateUtils.formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }
}
